package com.pjmike.netty.controller;

import com.alibaba.fastjson.JSONObject;
import com.pjmike.netty.client.NettyMessageUtil;
import com.pjmike.netty.entity.User;
import com.pjmike.netty.protocol.message.packet.response.AuthResponsePacket;
import com.pjmike.netty.server.http.web.annotation.*;
import com.pjmike.netty.server.http.web.rest.HttpStatus;
import com.pjmike.netty.server.http.web.rest.ResponseEntity;
import com.pjmike.netty.server.service.HeartBeatMessageService;
import org.springframework.beans.factory.annotation.Autowired;

import javax.validation.Valid;

//默认为单例，singleton = false表示启用多例。
//@RestController(singleton = false)
@RestController
@RequestMapping("/user")
public class UserController {
    @Autowired
    private NettyMessageUtil nettyMessageUtil;

    @PostMapping("list")
    @JsonResponse
    public ResponseEntity<User> listUser(@Valid @RequestBody User u) {
        // 查询用户
        User user = new User();
        user.setId(1);
        user.setName("");
        user.setAge((short)18);
        return ResponseEntity.ok().build(user);
    }

    @GetMapping("sendMsg")
    @JsonResponse
    public ResponseEntity<String> sendMsg() {
        AuthResponsePacket authResponsePacket = new AuthResponsePacket();
        authResponsePacket.setSbbh("310104000271101");
        authResponsePacket.setApply((short)1);
        nettyMessageUtil.sendMsg("310104000271101",authResponsePacket);
        return ResponseEntity.ok().build("成功");
    }

    @PutMapping("/{id}")
    public ResponseEntity<?> putMethod(@PathVariable("id") int id, @RequestBody String body) {
        // 更新用户
        return ResponseEntity.status(HttpStatus.CREATED).build();
    }
    
    @DeleteMapping("/{id}")
    public ResponseEntity<?> deleteMethod(@PathVariable int id) {
        // 删除用户
        return ResponseEntity.status(HttpStatus.NO_CONTENT).build();
    }


}
